package com.arvato.right.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.TreeBean;
import com.arvato.right.bean.bean_do.WxworkRightResourceDo;
import com.arvato.right.bean.bean_vo.WxworkRightResourceVo;
import com.arvato.right.bean.bean_vo.WxworkRightUserRoleVo;
import com.arvato.right.mapper.WxworkRightResourceMapper;
import com.arvato.right.mapper.WxworkRightUserRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class WxworkRightResourceService {
	
	@Autowired
	private WxworkRightResourceMapper wxworkRightResourceMapper;
	
	@Autowired
	private WxworkRightUserRoleMapper userRoleMapper;
	/**
	 * 按照条件查询多个资源(分页)
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageData listResourceByQuery(Query query, Integer pageNum, Integer pageSize) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		// 执行查询
		List<WxworkRightResourceVo> userList = wxworkRightResourceMapper.listResourceByQuery(query);
		// 得到结果
		PageInfo<WxworkRightResourceVo> pageInfo = new PageInfo<WxworkRightResourceVo>(userList);
		PageData data = new PageData(pageInfo.getTotal(), pageInfo.getList());
		return data;
	}
	
	/**
	 * 按照条件查询多个资源(不分页)
	 * @param query
	 * @return
	 */
	public List<WxworkRightResourceVo> listResourceByQuery(Query query) {
		// 执行查询
		List<WxworkRightResourceVo> userList = wxworkRightResourceMapper.listResourceByQuery(query);
		return userList;
	}
	
	/**
	 * 得到parentId下的所有资源接成的资源树
	 * @param parentId 
	 * @return
	 */
	public List<TreeBean> getResourceTree(String parentId) {
		return getTree(parentId, null, "check");
	}
	
	/**
	 * 根据ID查询资源
	 * @param resourceId
	 * @return
	 */
	public WxworkRightResourceVo getResouceById(String resourceId){
		return wxworkRightResourceMapper.getResourceById(resourceId);
	}
	
	/**
	 * 根据角色查询资源列表，得到一个角色的所有资源
	 * @param roleId
	 * @return
	 */
	public List<WxworkRightResourceVo> listResourceByRoleId(String roleId) {
		return wxworkRightResourceMapper.listResourceByRoleId(roleId);
	}
	
	/**
	 * 根据roleId查询资源树，其中改roleId拥有的权限，默认选中
	 * @param roleId
	 * @return
	 */
	public List<TreeBean> getResourceTreeByRoleId(String roleId) {
		List<WxworkRightResourceVo> resourceList = listResourceByRoleId(roleId);
		return getTree("0", resourceList, "checked");
	}
	
	/**
	 * 根据用户查询资源，查询一个用户的所有资源
	 * @return
	 */
	public List<WxworkRightResourceVo> listResourceByUserId(String userId) {
		List<WxworkRightResourceVo> resourceList = new ArrayList<WxworkRightResourceVo>();
		Query query = new Query();
		query.put("userId", userId);
		List<WxworkRightUserRoleVo> userRoleVoList = userRoleMapper.listByQuery(query);
		for(WxworkRightUserRoleVo userRoleVo : userRoleVoList){
			List<WxworkRightResourceVo> resourceVoListt = wxworkRightResourceMapper.listResourceByRoleId(userRoleVo.getRoleId());
			for(WxworkRightResourceVo resourceVo : resourceVoListt){
				if(!resourceList.contains(resourceVo)){
					resourceList.add(resourceVo);
				}
			}
		}
		return resourceList;
	}
	
	/**
	 * 根据用户查询资源树，查询一个用户所拥有的资源树
	 * @return
	 */
	public List<TreeBean> getTreeByUserId(String userId) {
		List<WxworkRightResourceVo> resourceList = listResourceByUserId(userId);
		return getTree("0", resourceList, "delete");

	}
	
	/**
	 * 查询数据库，拼接资源树
	 * @param parentId 
	 * @param resourceList 
	 * @param type 不同的type用不同的逻辑来处理resourceList
	 * 				type=delete 则resourceList中不包含的resource都被删除
	 * 				type=checked 则resourceList中含的resource都被checked
	 * @return
	 */
	private List<TreeBean> getTree(String parentId, List<WxworkRightResourceVo> resourceList, String type) {
		List<TreeBean> treeList = new ArrayList<TreeBean>();
		// 先查出来最顶级部门
		Query query = new Query();
		query.put("parentId", parentId);
		List<WxworkRightResourceVo> resList = wxworkRightResourceMapper.listResourceByQuery(query);
		for(WxworkRightResourceVo resource : resList){
			TreeBean tree = new TreeBean();
			if(type.equals("delete") && !resourceList.contains(resource) && resource.getParentId().equals("super_resource")) {
				continue;
			}else if("type".equals("checked")) {
				if(resourceList != null && resourceList.contains(resource)){
					tree.setChecked(true);
				}
			}
			tree.setId(resource.getResourceId());
			tree.setName(resource.getResourceName());
			tree.setPid(resource.getParentId());
			tree.setLink(resource.getResourceUrl());
			BeanUtils.copyProperties(resource, tree);
			treeList.add(tree);
			joinChilds(tree, treeList, resourceList, type);
		}
		return treeList;
	}
	
	/**
	 * 递归添加子节点
	 * @param tree 
	 * @param treeList
	 * @param resourceList 
	 * @param type 的用法同getTree()方法的type
	 * @param query
	 */
	private void joinChilds(TreeBean tree, List<TreeBean> treeList, List<WxworkRightResourceVo> resourceList, String type) {
		Query query = new Query();
		query.put("parentId", tree.getId());
		List<WxworkRightResourceVo> resList = wxworkRightResourceMapper.listResourceByQuery(query);
		for(WxworkRightResourceVo resource : resList){
			TreeBean childTree = new TreeBean();
			if(type.equals("delete") && !resourceList.contains(resource) && resource.getParentId().equals("super_resource")) {
				continue;
			}if(type.equals("checked")) {
				if(resourceList != null && resourceList.contains(resource)){
					tree.setChecked(true);
				}
			}
			childTree.setId(resource.getResourceId());
			childTree.setName(resource.getResourceName());
			childTree.setPid(resource.getParentId());
			childTree.setLink(resource.getResourceUrl());
			BeanUtils.copyProperties(resource, childTree);
			treeList.add(childTree);
			joinChilds(childTree, treeList, resourceList, type);
		}
	}
	
	/**
	 * 修改资源的状态
	 * @param resource
	 * @return
	 */
	public int update(WxworkRightResourceDo resource) {
		return wxworkRightResourceMapper.update(resource);
	}
	
	/**
	 * 插入一个资源
	 * @param resourcDo
	 * @return
	 */
	public int insert(WxworkRightResourceDo resourcDo) {
		return wxworkRightResourceMapper.insert(resourcDo);
	}

	/**
	 * 删除一个资源
	 * @param resourceId
	 * @return
	 */
	public int delete(String resourceId) {
		return wxworkRightResourceMapper.delete(resourceId);
	}
	
	
}
